package edu.university.demo.core.kits;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * 文件上传
 * 
 * 封装 Apache Commons FileUpload API
 *  
 * @author mayanping
 */
public class UploadedFile{

	/**
	 * 上传MP3文件
	 * 默认缓冲区1M，默认文件最大限制100M 
	 * */
	public static boolean uploadMP3(HttpServletRequest request,String fileContextPath){
		return upload(request,"mp3",fileContextPath);
	}
	/**
	 * 上传图片文件
	 * 默认缓冲区1M，默认文件最大限制10M
	 * */
	public static boolean uploadIMG(HttpServletRequest request,String fileContextPath){
		return upload(request,1024*1024*10,"jpg,gif,bmp",fileContextPath);
	}

	/**
	 * 上传文件
	 * 默认缓冲区1M，默认文件最大限制100M
	 * @param request 请求
	 * @param fileType 文件类型
	 * @param fileContextPath 文件相对路径
	 * */
	public static boolean upload(HttpServletRequest request,String fileType,String fileContextPath){
		return upload(request,1024*1024*100,fileType,fileContextPath);
	}
	
	/**
	 * 上传文件
	 * 默认缓冲区1M
	 * @param request 请求
	 * @param fileMaxSize 文件最大限制
	 * @param fileType 文件类型
	 * @param fileContextPath 文件相对路径
	 * */
	public static boolean upload(HttpServletRequest request,int fileMaxSize,String fileType,String fileContextPath){
		return upload(request,1024*1024,fileMaxSize,fileType,fileContextPath);
	}
	/**
	 * 上传文件
	 * @param request 请求
	 * @param bufferSize 缓冲区大小
	 * @param fileMaxSize 文件最大限制
	 * @param fileType 文件类型
	 * @param fileContextPath 文件相对路径
	 * @return true 上传成功
	 * */
	public static boolean upload(HttpServletRequest request,int bufferSize,int fileMaxSize,String fileType,String fileContextPath){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		String basePath = request.getServletContext().getRealPath(fileContextPath);
		File repository = new File(basePath); // 缓存区目录
		if(fileType == null || "".equals(fileType)){ 
			fileType = "jpg,gif,bmp,rar,rar,txt,docx,mp3,flac";
		}
		DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
		diskFileItemFactory.setRepository(repository);
		//设置缓冲区大小
		diskFileItemFactory.setSizeThreshold(bufferSize);
		ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
		//设置文件大小
		servletFileUpload.setSizeMax(fileMaxSize);

		List<FileItem> fileItems = null;
		try {
			fileItems = servletFileUpload.parseRequest(request);

			for (FileItem fileItem : fileItems) {
				String filePath = fileItem.getName();
				System.out.println(filePath);
				if (filePath == null || filePath.trim().length() == 0)
					continue;
				String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
				String extName = filePath.substring(filePath.lastIndexOf(".") + 1);
				if (fileType.contains(extName)) {
					try {
						fileItem.write(new File(basePath + File.separator + fileName));
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					throw new FileUploadException("不支持该文件类型!");
				}
			}
		} catch (FileSizeLimitExceededException e) {
			System.out.println("文件大小超限!");
			return false;
		} catch (FileUploadException e1) {
			e1.printStackTrace();
			return false;
		}
		return true;
	}
}
